using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Forum.Business
{
    public class MyOpenThreadList : ForumUserControl,
        IReplyConverter<ThreadReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;

        public bool CanEditMyThread
        {
            get
            {
                object o = ViewState["CanEditMyThread"];
                if (o != null)
                {
                    return bool.Parse(o.ToString());
                }
                return false;
            }
            set
            {
                ViewState["CanEditMyThread"] = value;
            }
        }
        public bool CanDeleteMyThread
        {
            get
            {
                object o = ViewState["CanDeleteMyThread"];
                if (o != null)
                {
                    return bool.Parse(o.ToString());
                }
                return false;
            }
            set
            {
                ViewState["CanDeleteMyThread"] = value;
            }
        }

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                requestBinders.Add(RequestHelper.BuildGetThreadListRequest(this, pager.PageSize));
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void BindData(ThreadReply reply)
        {
            if (list == null)
            {
                return;
            }

            if (reply.EntityList.Count > 0)
            {
                if (ValidatePermission(PermissionType.EditThread, reply.EntityList[0]))
                {
                    CanEditMyThread = true;
                }
                if (ValidatePermission(PermissionType.DeleteThread, reply.EntityList[0]))
                {
                    CanDeleteMyThread = true;
                }
            }

            list.DataSource = reply.EntityList;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(reply.TotalRecords);
                currentPage.TotalRecords = reply.TotalRecords;
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region Ajax Method

        [AjaxMethod]
        public void DeleteThread(int threadId)
        {
            Thread thread = BusinessManager.GetThread(threadId);

            if (!ValidatePermission(PermissionType.DeleteThread, thread))
            {
                throw new Exception("Operation denied.");
            }

            BusinessManager.DeleteThread(threadId);
        }

        #endregion

        #region IReplyConverter<ThreadReply,GetListOperation> Members

        public void ConvertReply(ThreadReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                BindData(reply);
            }
        }

        #endregion
    }
}